package br.edu.univas.locadoragraph.dao;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;

import br.edu.univas.locadoragraph.database.Connection;
import br.edu.univas.locadoragraph.database.Keys;
import br.edu.univas.locadoragraph.to.MovieTO;
import br.edu.univas.locadoragraph.to.UserTO;


public class UserDAO {

	private Connection conn;

	public UserDAO() {
		conn = Connection.getInstance();
	}

	public boolean veryfyUserLogin(UserTO user) {
		long start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();

		Index<Node> index = graph.index().forNodes(Keys.INDEXUSER);

		Node node = index.get(Keys.USERKEY, user.getEmail().toLowerCase()).getSingle();
		System.out.println("Node login: " + node);
		if (node != null) {
			System.out.println("Checando senha");
			if (user.getPassword().equals(node.getProperty("password"))) {
				System.out.println("senha correta");
				//graph.shutdown();
				double total = System.currentTimeMillis() - start;
				System.out.println("Time: " + total/1000);
				return true;
			}
		}
		//graph.shutdown();
		double total = System.currentTimeMillis() - start;
		System.out.println("Time: " + total/1000);
		return false;
	
	}

	public boolean saveUserGraph(UserTO user) {
		System.out.println("Method: save user");
		long start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Index<Node> index = graph.index().forNodes(Keys.INDEXUSER);
		Node node = index.get(Keys.USERKEY, user.getEmail().toLowerCase()).getSingle();
		if (node == null) {
			Transaction tx = graph.beginTx();
			try {
				node = graph.createNode();
				node.setProperty(Keys.USERKEY, user.getEmail().toLowerCase());
				node.setProperty("name", user.getName().toLowerCase());
				node.setProperty("password", user.getPassword());
				index.add(node, Keys.USERKEY, user.getEmail().toLowerCase());
				tx.success();
				tx.finish();
				//graph.shutdown();
				double total = System.currentTimeMillis() - start;
				System.out.println("User saved! Time: " + total/1000);
				return true;
			} catch (Exception e) {
				tx.failure();
				tx.finish();
			} 
		}
		//graph.shutdown();
		double total = System.currentTimeMillis() - start;
		System.out.println("User not saved! Time: " + total/1000);
		return false;

	}
	public boolean setNoteMovie(String emailUser, String nomeFilme, int note){
		System.out.println("Email: "+emailUser+ "Filme: "+nomeFilme+" Nota: "+note);
		return true;
	}

	public Node getNodeUser(GraphDatabaseService graph, String user) {
		Index<Node> index = graph.index().forNodes(Keys.INDEXUSER);
		Node node = index.get(Keys.USERKEY, user.toLowerCase()).getSingle();
		return node;
	}
	
	public List<UserTO> getAllUsers(){
		System.out.println("Method: get all users");
		List<UserTO> users = new ArrayList<UserTO>();
		double start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Index<Node> index = graph.index().forNodes(Keys.INDEXUSER);
		Iterable<Node> nodes = index.query(Keys.USERKEY, "*");
		for (Node node : nodes) {
			UserTO user = new UserTO();
			user.setEmail((String) node.getProperty(Keys.USERKEY));
			user.setName((String) node.getProperty("name"));
			users.add(user);
		}
		
		double total = System.currentTimeMillis() - start;
		System.out.println("Time: " + total/1000);
		return users;
	}
	
	public boolean updateUser(String email, UserTO user){
		System.out.println("Method: update user");
		double start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Node node = getNodeUser(graph, email);
		
		if (node != null) {
			Transaction tx = graph.beginTx();
			try {
				node.setProperty(Keys.USERKEY, user.getEmail().toLowerCase());
				node.setProperty("name", user.getName().toLowerCase());
				node.setProperty("password", user.getPassword());
				double total = System.currentTimeMillis() - start;
				System.out.println("Time: " + total/1000);
				tx.success();
				tx.finish();
				return true;
				
			} catch (Exception e) {
				System.err.println("Erro: " + e);
				tx.failure();
				tx.finish();
			}
		}
		double total = System.currentTimeMillis() - start;
		System.out.println("Time: " + total/1000);
		return false;
	}
	
	public boolean watchedMovie(UserTO user, String movie) {
		double start = System.currentTimeMillis();
		GraphDatabaseService graph = conn.newGraphDatabase();
		Node node = getNodeUser(graph, user.getEmail());
		if (node != null) {
			Transaction tx = graph.beginTx();
			try {
				if(RelationshipGraph.createRelationshipUserMovie(graph, node , movie)){
					tx.success();
					tx.finish();
					double total = System.currentTimeMillis() - start;
					System.out.println("Relacionamento concluído. Time: " + total/1000);
					return true;
				}
			} catch (Exception e) {
				System.err.println("Erro: " + e);
				tx.success();
				tx.finish();
			}
		}
		double total = System.currentTimeMillis() - start;
		System.out.println("Relacionamento concluído. Time: " + total/1000);
		return false;
		
	}

}